<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\admin\model\ProvCityArea;
use app\admin\model\SellGoodsBrandAdress;
use app\admin\model\SellGoodsRefund;

class BigbrandController extends CommonController {
    /*
     * 大牌商品活动列表
     * author xfwed 
     * date 2020.11.24
     */

    public function bigBrandList() {
        $activity_name = input('param.activity_name');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($activity_name) {
            $where['activity_name'] = ['like', '%' . $activity_name . '%'];
        }
        $where = isset($where)?$where:[];
        
        $list = Db::table('goods_activity')
                ->where($where)
                ->order('end_time asc')
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 添加大牌商品活动
     * author xfwed 
     * date 2020.11.24
     */

    public function addBigBrand() {
        $rule = [
            'activity_name' => 'require',
            'start_time' => 'require',
            'end_time' => 'require',
        ];
        $msg = [
            'activity_name.require' => '活动名称必须',
            'start_time.require' => '开始时间必须',
            'end_time.require' => '结束时间必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $activity_name = $all['activity_name'];
        $start_time = $all['start_time'];
        $end_time = $all['end_time'];
        $remarks = input('param.remarks');
        $activityRow = Db::table('goods_activity')
                ->where('is_del',2)
                ->where("('".$start_time."' between start_time and end_time) or ('".$end_time."' between start_time and end_time) or (start_time >'".$start_time."' and end_time < '".$end_time."')")
                ->find();
        if($activityRow){
            return $this->ajaxReturn(3, '时间冲突，请重新选择时间', null);
        }
        
        if($start_time >= $end_time){
            return $this->ajaxReturn(3, '结束时间不能小于开始时间', null);
        }

        $data['type'] = 1;
        $data['activity_name'] = $activity_name;
        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['remarks'] = $remarks;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        Db::table('goods_activity')->insert($data);
        return $this->ajaxReturn(0, '添加成功', null);
    }
    
    /*
     * 删除大牌商品活动
     * author xfwed 
     * date 2020.11.24
     */

    public function delBigBrand() {
        $rule = [
            'id' => 'require',
        ];
        $msg = [
            'id.require' => '活动id必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $id = $all['id'];
        
        $activityRow = Db::table('goods_activity')
                ->where('id',$id)
                ->find();
        $now = date('Y-m-d H:i:s',time());
        if($activityRow['start_time'] < $now && $activityRow['end_time'] > $now){
            return $this->ajaxReturn(3, '活动进行中，不支持删除', null);
        }
        Db::table('goods_activity')->where('id',$id)
                ->update(['is_del'=>1]);
        return $this->ajaxReturn(0, '删除成功', null);
    }
    
    /*
     * 大牌活动商品列表
     * author xfwed 
     * date 2020.11.24
     */

    public function bigBrandGoodsList() {
        $goods_activity_id = input('param.goods_activity_id');
        $goods_name = input('param.goods_name');
        $is_del = input('param.is_del');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($goods_name) {
            $where['ga.goods_name'] = ['like', '%' . $goods_name . '%'];
        }
        if ($is_del) {
            $where['ga.is_del'] = $is_del;
        }
        $where['ga.goods_activity_id'] = $goods_activity_id;
        
        $list = Db::table('goods_activity_big_brand')
                ->alias('ga')
                ->join(['goods_info'=>'i'],'i.id = ga.gift_goods_id','left')
                ->where($where)
                ->field('ga.id,ga.goods_name,ga.price,ga.counter_price,ga.group_num,ga.advance_money,ga.gift_goods_desc,ga.finishPay_startTime,ga.finishPay_endTime,ga.is_del,ga.add_time,i.name')
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 添加大牌活动商品
     * author xfwed 
     * date 2020.11.24
     */

    public function addBigBrandGoods() {
        $rule = [
            'goods_activity_id' => 'require',
            'goods_name' => 'require',
            'goods_id' => 'require',
            'price' => 'require',
            'counter_price' => 'require',
            'group_num' => 'require',
            'advance_money' => 'require',
//            'gift_goods_desc' => 'require',
//            'gift_goods_id' => 'require',
            'finishPay_startTime' => 'require',
            'finishPay_endTime' => 'require',
        ];
        $msg = [
            'goods_activity_id.require' => '活动id必须',
            'goods_name.require' => '商品名称必须',
            'goods_id.require' => '商品id必须',
            'price.require' => '平台价必须',
            'counter_price.require' => '专柜价必须',
            'group_num.require' => '成团人数必须',
            'advance_money.require' => '预付金必须',
//            'gift_goods_desc.require' => '赠品描述必须',
//            'gift_goods_id.require' => '赠品id必须',
            'finishPay_startTime.require' => '付尾款开始时间必须',
            'finishPay_endTime.require' => '付尾款结束时间必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $goods_activity_id = $all['goods_activity_id'];
        $goods_name = $all['goods_name'];
        $goods_id = $all['goods_id'];
        $price = $all['price'];
        $counter_price = $all['counter_price'];
        $group_num = $all['group_num'];
        $advance_money = $all['advance_money'];
        $gift_goods_desc = $all['gift_goods_desc'];
        $gift_goods_id = $all['gift_goods_id'];
        $finishPay_startTime = $all['finishPay_startTime'];
        $finishPay_endTime = $all['finishPay_endTime'];
        
        
        $activityRow = Db::table('goods_activity_big_brand')
                ->where('goods_activity_id',$goods_activity_id)
                ->where('goods_id',$goods_id)
                ->where('is_del',2)
                ->find();
        if($activityRow){
            return $this->ajaxReturn(3, '该活动已经包含此商品', null);
        }
        
        if($finishPay_startTime >= $finishPay_endTime){
            return $this->ajaxReturn(3, '结束时间不能小于开始时间', null);
        }

        $data['goods_activity_id'] = $goods_activity_id;
        $data['goods_name'] = $goods_name;
        $data['goods_id'] = $goods_id;
        $data['price'] = $price;
        $data['counter_price'] = $counter_price;
        $data['group_num'] = $group_num;
        $data['advance_money'] = $advance_money;
        $data['gift_goods_desc'] = $gift_goods_desc;
        $data['gift_goods_id'] = $gift_goods_id;
        $data['finishPay_startTime'] = $finishPay_startTime;
        $data['finishPay_endTime'] = $finishPay_endTime;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        Db::table('goods_activity_big_brand')->insert($data);
        return $this->ajaxReturn(0, '添加成功', null);
    }
    
    /*
     * 修改大牌活动商品
     * author xfwed 
     * date 2020.11.25
     */

    public function saveBigBrandGoods() {
        $requestType = input('param.requestType');
        $id = input('param.id');
        if($requestType == 1){//1修改前获取信息 2提交修改
            $row = Db::table('goods_activity_big_brand')
                    ->alias('gabb')
                    ->join(['goods_info'=>'gi'],'gi.id = gabb.goods_id','left')
                    //->join(['goods_info'=>'gin'],'gin.id = gabb.gift_goods_id','left')
                    ->where('gabb.id',$id)
                    ->field('gabb.*,gi.name selectGoodsName')
                    ->find();
            $row['gift_goods_desc'] = explode("|", $row['gift_goods_desc']);
            $row['gift_goods_id'] = explode("|", $row['gift_goods_id']);
            return $this->ajaxReturn(0, '查询成功', $row);
        }  else {
            $rule = [
            'goods_activity_id' => 'require',
            'goods_name' => 'require',
            'goods_id' => 'require',
            'price' => 'require',
            'counter_price' => 'require',
            'group_num' => 'require',
            'advance_money' => 'require',
            //'gift_goods_desc' => 'require',
            //'gift_goods_id' => 'require',
            'finishPay_startTime' => 'require',
            'finishPay_endTime' => 'require',
        ];
        $msg = [
            'goods_activity_id.require' => '活动id必须',
            'goods_name.require' => '商品名称必须',
            'goods_id.require' => '商品id必须',
            'price.require' => '平台价必须',
            'counter_price.require' => '专柜价必须',
            'group_num.require' => '成团人数必须',
            'advance_money.require' => '预付金必须',
            //'gift_goods_desc.require' => '赠品描述必须',
            //'gift_goods_id.require' => '赠品id必须',
            'finishPay_startTime.require' => '付尾款开始时间必须',
            'finishPay_endTime.require' => '付尾款结束时间必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        
        $goods_activity_id = $all['goods_activity_id'];
        $goods_name = $all['goods_name'];
        $goods_id = $all['goods_id'];
        $price = $all['price'];
        $counter_price = $all['counter_price'];
        $group_num = $all['group_num'];
        $advance_money = $all['advance_money'];
        $gift_goods_desc = $all['gift_goods_desc'];
        $gift_goods_id = $all['gift_goods_id'];
        $finishPay_startTime = $all['finishPay_startTime'];
        $finishPay_endTime = $all['finishPay_endTime'];
        
        
        $activityRow = Db::table('goods_activity_big_brand')
                ->where('goods_activity_id',$goods_activity_id)
                ->where('goods_id',$goods_id)
                ->where('is_del',2)
                ->where('id','<>',$id)
                ->find();
        if($activityRow){
            return $this->ajaxReturn(3, '该活动已经包含此商品', null);
        }
        
        if($finishPay_startTime >= $finishPay_endTime){
            return $this->ajaxReturn(3, '结束时间不能小于开始时间', null);
        }
        
        $goodsacRow = Db::table('goods_activity')->where('id',$goods_activity_id)->find();
        $now = date('Y-m-d H:i:s',time());
        if($goodsacRow['start_time']<$now && $goodsacRow['end_time']>$now){
            //return $this->ajaxReturn(3, '活动进行中，不允许修改商品', null);
        }

        $data['goods_activity_id'] = $goods_activity_id;
        $data['goods_name'] = $goods_name;
        $data['goods_id'] = $goods_id;
        $data['price'] = $price;
        $data['counter_price'] = $counter_price;
        $data['group_num'] = $group_num;
        $data['advance_money'] = $advance_money;
        $data['gift_goods_desc'] = $gift_goods_desc;
        $data['gift_goods_id'] = $gift_goods_id;
        $data['finishPay_startTime'] = $finishPay_startTime;
        $data['finishPay_endTime'] = $finishPay_endTime;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        Db::table('goods_activity_big_brand')->where('id',$id)->update($data);
        return $this->ajaxReturn(0, '修改成功', null);
        }
    }

    /*
     * 删除大牌活动商品
     * author xfwed 
     * date 2020.11.25
     */

    public function delBigBrandGoods() {
        $id = input('param.id');

        $activityRow = Db::table('goods_activity_big_brand')
                ->where('id','=',$id)
                ->find();
        $goodsacRow = Db::table('goods_activity')->where('id',$activityRow['goods_activity_id'])->find();
        $now = date('Y-m-d H:i:s',time());
        if($goodsacRow['start_time']<$now && $goodsacRow['end_time']>$now){
            return $this->ajaxReturn(3, '活动进行中，不允许删除商品', null);
        }

        $data['is_del'] = 1;
        
        Db::table('goods_activity_big_brand')->where('id',$id)->update($data);
        return $this->ajaxReturn(0, '删除成功', null);
        
    }
    
    /*
     * 拼团列表
     * author xfwed 
     * date 2020.11.25
     */

    public function goodsGroupList() {
        $id = input('param.id');//这是 goods_activity_big_brand 表里的id
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $goodsGroupList = Db::table('goods_group')
                ->alias('gg')
                ->join(['goods_info'=>'gi'],'gi.id = gg.goods_id','left')
                ->where('gg.big_brand_id',$id)
                ->field('gg.*,gi.name')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $goodsGroupList);
        
    }
    
    /*
     * 拼团详情列表
     * author xfwed 
     * date 2020.11.25
     */

    public function goodsGroupInfoList() {
        $id = input('param.id');//这是 goods_group 表里的id
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        
        $goodsGroupList = Db::table('goods_group_detail')
                ->alias('ggd')
                ->join('custom c','c.id = ggd.ctm_id','left')
                ->join(['pay_order_detail'=>'pod'],'pod.id = ggd.order_detail_id','left')
                ->where('ggd.group_id',$id)
                ->field('ggd.id,ggd.status,ggd.add_time,c.custom_name,c.custom_phone,pod.order_id')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $goodsGroupList);
    }
    
    /*
     * 想要大牌商品列表
     * author xfwed 
     * date 2020.12.01
     */

    public function wantBigBrandList() {
        $goods_name = input('param.goods_name');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if ($goods_name) {
            $where['gw.goods_name'] = ['like', '%' . $goods_name . '%'];
        }
        $where = isset($where)?$where:[];
        
        $list = Db::table('goods_want')
                ->alias('gw')
                ->join('custom c','c.id = gw.ctm_id','left')
                ->field('gw.*,c.custom_name,c.custom_phone,c.custom_photo')
                ->where($where)
                ->order('id desc')
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $list);
    }
}
